package cn.bytecollege.merchant.controller;

import cn.bytecollege.common.entity.Merchant;
import cn.bytecollege.common.entity.User;
import cn.bytecollege.common.util.MD5Util;
import cn.bytecollege.merchant.service.MerchantService;
import cn.bytecollege.user.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

@WebServlet(name = "MerchantRegisterController",value = "/merchant/register")
public class MerchantRegisterController extends HttpServlet {
    private MerchantService merchantService = null;
    public MerchantRegisterController(){
        merchantService = new MerchantService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/admin/merchantRegister.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收form表单传的数据
        String username= request.getParameter("username");
        String password = request.getParameter("password");
        String merchantParticulars = request.getParameter("merchantParticulars");

        //封装数据
        Merchant merchant = new Merchant();
        merchant.setMerchantName(username);
        merchant.setMerchantPassword(MD5Util.md5Encode(password));
        merchant.setMerchantParticulars(merchantParticulars);



        merchantService.save(merchant);
        //注册成功后跳转到登录
        response.sendRedirect("/merchant/login");
    }
}
